import Vue from 'vue'
import VueRouter from 'vue-router'

// 1. 先导入需要的路由组件
// import Reg from '@/views/Reg/Reg.vue'
const Reg = () => import('@/views/Reg/Reg.vue')
// import Login from '@/views/Login/Login.vue'
const Login = () => import('@/views/Login/Login.vue')
// import Main from '@/views/Main/Main.vue'
const Main = () => import('@/views/Main/Main.vue')
// 导入子路由对应的组件
// import Home from '@/views/menus/Home/Home.vue'
const Home = () => import('@/views/menus/Home/Home.vue')
// import UserInfo from '@/views/menus/User/UserInfo.vue'
const UserInfo = () => import('@/views/menus/User/UserInfo.vue')
// import UserAvatar from '@/views/menus/User/UserAvatar.vue'
const UserAvatar = () => import('@/views/menus/User/UserAvatar.vue')
// import UserPwd from '@/views/menus/User/UserPwd.vue'
const UserPwd = () => import('@/views/menus/User/UserPwd.vue')
// import ArtCate from '@/views/menus/Article/ArtCate.vue'
const ArtCate = () => import('@/views/menus/Article/ArtCate.vue')
// import ArtList from '@/views/menus/Article/ArtList.vue'
const ArtList = () => import('@/views/menus/Article/ArtList.vue')

// 安装路由插件
Vue.use(VueRouter)
// 把下面的代码粘贴到路由模块中对应的位置，即可防止路由报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 2. 在数组中声明路由规则
const routes = [
  // 注册的路由规则
  {
    path: '/reg',
    component: Reg
  },
  // 登录的路由规则
  {
    path: '/Login',
    component: Login
  },
  // 后台主页的路由规则
  {
    path: '/',
    component: Main,
    redirect: '/home',
    children: [{
      path: 'home',
      component: Home
    }, {
      path: 'user-info',
      component: UserInfo
    }, {
      path: 'user-avatar',
      component: UserAvatar
    }, {
      path: 'user-pwd',
      component: UserPwd
    }, {
      path: 'art-cate',
      component: ArtCate
    }, {
      path: 'art-list',
      component: ArtList
    }]
  }
]

const router = new VueRouter({
  routes
})

export default router
